#include "utils.h"
 #include <QNetworkInterface>
#include <QDebug>

QString mac2str(uchar *mac)
{
    char buf[128]{0};
    sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
    return buf;
}

QString ip2str(uchar *ip)
{
    char buf[128]{0};
    sprintf(buf, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
    return buf;
}

QString get_mac_by_ip(const QString &ip)
{
    if(ip.isEmpty()) return "";
    foreach (QNetworkInterface interface, QNetworkInterface::allInterfaces()) {
        foreach (QNetworkAddressEntry entry, interface.addressEntries()) {
            if(entry.ip().toString() == ip) {
                return interface.hardwareAddress();
            }
        }
    }
    return QString(); // 未找到返回空字符串
}